<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>问题管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="课程问题名称">
					<el-input v-model="queryInfo.problemTitle" clearable @clear="queryList" placeholder="请输
入课程问题名称"></el-input>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddArticle">新增问题</el-button>
					<el-button type="danger" @click="deleteBatch">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="articleList" border style="width: 100%" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID"></el-table-column>
			<el-table-column prop="problemTitle" label="问题标题"></el-table-column>
			<el-table-column prop="problemText" label="问题内容"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
			<el-table-column prop="user_id" label="用户id"></el-table-column>
			
			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditArticle(scope.row)">编辑问题</el-link>
					<el-link type="primary"  @click="deleteArticle(scope.row.id)">删除问题</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>


		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="article">
				<el-form-item label="问题标题" label-width="80px">
					<el-input placeholder="请输入问题标题" v-model="article.problemTitle"></el-input>
				</el-form-item>
				<el-form-item label="问题内容" label-width="80px">
					<!-- 富文本编辑器组件 -->
					<quill-editor v-model="article.problemText"></quill-editor>
					
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addArticle">确 定</el-button>
			</div>
		</el-dialog>


	</div>
</template>
<script>
	export default {
		data() {
			return {
				// 查询信息对象
				queryInfo: {
                    problemTitle:'',
					articleName: '',
					pageNum: 1,
					pageSize: 10
				},
				total: 183,
				articleList: [],
				title: '', // 对话框标题
				dialogFormVisible: false, // 是否显示对话框
				article: { // 文章对象
					"id": '',
					"problemTitle":'',
                    "problemText":'',
                    "createTime":'',
                    "user_id":''
				},
				dialogImageUrl: '', // 预览图片路径
				dialogVisible: false, // 预览图片的对话框
				fileList:[],      // 点击修改时，图片的预览效果
			}
		},
		mounted() {
			this.queryList();
		},
		methods: {
			
			

			// 取消对话框
			cancle() {
				this.dialogFormVisible = false;
				this.article = { // 文章对象
					"id": '',
					"problemTitle":'',
                    "problemText":'',
                    "createTime":'',
                    "user_id":''
				}
				this.fileList=[]
			},
			// 进入修改的预览数据页面
			toEditArticle(row) {
				//console.log(row)
				// 赋值
				this.article = row;
				// 设置标题
				this.title = "修改问题";
				// 开启对话框
				this.dialogFormVisible = true;

			},
			// 添加文章
			addArticle() {
				if (this.article.id) { // 有值为true  修改
					this.$http.put("consultproblem/edit", this.article)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('修改成功！');
								// 清空数据
								this.article = { // 文章对象
                                    "id": '',
                                    "problemTitle": '',
                                    "problemText": '',
                                    "createTime": '',
                                    "user_id": ''
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
								this.fileList=[]
							} else {
								this.$message.error('接口异常！')
							}
						})

				} else {
					this.$http.post("consultproblem/add", this.article)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('添加成功！');
								// 清空数据
								this.article = { // 文章对象
                                    "id": '',
                                    "problemTitle": '',
                                    "problemText": '',
                                    "createTime": '',
                                    "user_id": ''
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
								this.fileList=[]
							} else {
								this.$message.error('接口异常！')
							}
						})
				}
			},deleteArticle(id) {
   this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
     confirmButtonText: '确定',
     cancelButtonText: '取消',
     type: 'warning'
  })
  .then(() => {
      this.$http.delete('consultproblem/remove/' + id)
          .then(resp => {
          if (resp.data.status == 200) {
             this.$message.success(resp.message);
              this.queryList()
           } else {
             this.$message.error(resp.message)
             this.queryList()
            }
           })
       })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }, handleSelectionChange(val) {
   this.ids = val.map(function (item) {
      return item.id;
    });
   console.log(this.ids);
 },
    deleteBatch() {
      if (this.ids.length == 0) {   // 没有选择文章列表
        return;
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http.delete('consultproblem/removeBatch?ids=' + this.ids)
            .then(resp => {
              if (resp.data.status == 200) {
                this.$message.success(resp.message)
                this.queryList()
              } else {
                this.$message.error(resp.message)
              }
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
			// 进入添加的对话框
			toAddArticle() {
				this.title = "新增问题";
				this.dialogFormVisible = true;
				// 把当前登录的用户的id赋值为文章的teacher_id
				this.article.teacherId = sessionStorage.getItem('userid');
			},
			// 分页查询信息的方法
			queryList() {
				// 调用后端接口来获取数据
				this.$http.get('consultproblem/find', {
						params: this.queryInfo
					})
					.then(resp => {
						if (resp.data.status == 200) {
							console.log(resp.data)
							this.articleList = resp.data.data.list;
							this.total = resp.data.data.total;
							console.log(this.articleList)
						} else {
							this.$message.error('查询失败！')
						}
					})
			},

			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			}
		}
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}

	/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
	.ql-container {
		min-height: 200px;
	}
</style>